package com.googlecode.gingerman.world.geometry;

import java.util.ArrayList;

public class Circle extends Polygon {
	private double radius;

	public Circle(Coordinates center, double radius) {
		super(center, new ArrayList<Vector>());
		this.radius = radius;
	}
	
	public double getRadius(){
		return radius;
	}

	public ArrayList<Vector> getVertices() {
		ArrayList<Vector> vertices = new ArrayList<Vector>();
		double verticesCount = Math.pow(radius, 1.5);
		for (int i = 0; i < verticesCount; i++) {
			Vector element = new Vector(360 / verticesCount, radius);
			vertices.add(element);
		}
		return vertices;
	}

	public void rotate(double angle) {
		// There is no point to rotate circle
	}

	public void scale(double scale) {
		radius = radius * scale;
	}

	public boolean containsPoint(Coordinates k) {
		boolean yn = false;
		if ((k.getX() * k.getX() + k.getY() * k.getY()) <= radius * radius)
			yn = true;
		return yn;
	}

	@Override
	public boolean intersectFigure(Figure figure) {
		if (figure instanceof Circle) {
			Circle k = (Circle) figure;
			boolean yn = false;
			Coordinates tmp1 = this.getCenter();
			Coordinates tmp2 = k.getCenter();
			if (Math.sqrt((tmp1.getX() - tmp2.getX())
					* (tmp1.getX() - tmp2.getX()) + (tmp1.getY() - tmp2.getY())
					* (tmp1.getY() - tmp2.getY())) < radius + k.radius)
				yn = true;
			return yn;
		} else if (figure instanceof Rectangle){
			Rectangle k = (Rectangle) figure;
			Vector tmp1 = new Vector(this.getCenter());
			Vector tmp2 = new Vector(k.getCenter());
			ArrayList<Vector> vertices=k.getVertices();
			if(tmp1.getY()+radius>=vertices.get(0).getY())
				if(tmp1.getY()-radius<=vertices.get(2).getY())
					if(tmp1.getX()+radius>=vertices.get(3).getX())
						if(tmp1.getX()-radius<=vertices.get(1).getX()){
							
							if(k.getAngle()==0)
								return true;
							
							if(tmp1.substract(tmp2).getLength()<=k.getHeigth()/2+radius || tmp1.substract(tmp2).getLength()<=k.getWidth()/2+radius)
								return true;
							
							if((tmp1.getY()==vertices.get(3).getY() && tmp1.getX()<=vertices.get(2).getX()) || (tmp1.getY()>=vertices.get(3).getY() && tmp1.getX()==vertices.get(2).getX()) || (tmp1.getY()==vertices.get(1).getY() && tmp1.getX()>=vertices.get(0).getX()) || (tmp1.getY()<=vertices.get(1).getY() && tmp1.getX()==vertices.get(0).getX()))
								return true;
							
							if(tmp1.getY()>vertices.get(3).getY() && tmp1.getX()<vertices.get(2).getX()){
								double side1=vertices.get(3).substract(vertices.get(2)).getLength();
								double side2=vertices.get(3).substract(tmp1).getLength();
								double side3=vertices.get(2).substract(tmp1).getLength();
								double angletriangle=Math.acos((side2*side2-side1*side1-side3*side3)/(side1*side3));
								if (angletriangle>Math.PI/2) angletriangle-=Math.PI/2;
								double heigthtriangle=side3*Math.sin(angletriangle);
								if (heigthtriangle<=radius)	return true;
								else return false;
							}
							
							if(tmp1.getY()<vertices.get(3).getY() && tmp1.getX()<vertices.get(0).getX()){
								double side1=vertices.get(0).substract(vertices.get(3)).getLength();
								double side2=vertices.get(0).substract(tmp1).getLength();
								double side3=vertices.get(3).substract(tmp1).getLength();
								double angletriangle=Math.acos((side2*side2-side1*side1-side3*side3)/(side1*side3));
								if (angletriangle>Math.PI/2) angletriangle-=Math.PI/2;
								double heigthtriangle=side3*Math.sin(angletriangle);
								if (heigthtriangle<=radius)	return true;
								else return false;
							}
							
							if(tmp1.getY()<vertices.get(1).getY() && tmp1.getX()>vertices.get(0).getX()){
								double side1=vertices.get(1).substract(vertices.get(0)).getLength();
								double side2=vertices.get(1).substract(tmp1).getLength();
								double side3=vertices.get(0).substract(tmp1).getLength();
								double angletriangle=Math.acos((side2*side2-side1*side1-side3*side3)/(side1*side3));
								if (angletriangle>Math.PI/2) angletriangle-=Math.PI/2;
								double heigthtriangle=side3*Math.sin(angletriangle);
								if (heigthtriangle<=radius)	return true;
								else return false;
							}
							
							if(tmp1.getY()>vertices.get(1).getY() && tmp1.getX()>vertices.get(2).getX()){
								double side1=vertices.get(1).substract(vertices.get(2)).getLength();
								double side2=vertices.get(1).substract(tmp1).getLength();
								double side3=vertices.get(2).substract(tmp1).getLength();
								double angletriangle=Math.acos((side2*side2-side1*side1-side3*side3)/(side1*side3));
								if (angletriangle>Math.PI/2) angletriangle-=Math.PI/2;
								double heigthtriangle=side3*Math.sin(angletriangle);
								if (heigthtriangle<=radius)	return true;
								else return false;
							}
							
						}
			
			return false;
		}
		else {
			return super.intersectFigure(figure);
		}

	}

}
